package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gulimall
 * @description: 处理社交登录请求!
 * @author: YuWenYi
 * @create: 2021-07-15 16:31
 **/
@Controller
@Slf4j
public class OAuth2Controller {
    @Autowired
    MemberFeignService memberFeignService;

    @RequestMapping("/oauth2/gitee/success")
    public String gitee(@RequestParam("code")String code, HttpSession session) throws Exception {
        System.out.println("进入gitee方法");

        //使用code去换取accessToken,登录成功就跳回首页!
        Map<String,String> map = new HashMap<>();
        map.put("grant_type", "authorization_code");
        map.put("code",code);
        map.put("client_id", "e0a9bfca4cb582678d4824dc663885453eaa747a7dfd084e366e1bf5c8751466");
        map.put("client_secret", "a8760dc595d1fcf254e275c3a15bdfe51e688b94af15a2403e3d1466c94b85c4");
        map.put("redirect_uri", "http://auth.gulimall.com/oauth2/gitee/success");

        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token",
                "POST", new HashMap<>(), map,new HashMap<>());

        //处理获取的相应数据
        if (response.getStatusLine().getStatusCode() == 200){
            String bodyJson = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(bodyJson, SocialUser.class);
            System.out.println("socialUser = " + socialUser);
            //如果用户是第一次登录进来,那么需要给这个用户生成一个本系统的账号!
            //以后此社交登录的用户就等于绑定了这个账号(这个账号才是商城的会员账号)

            //远程调用member微服务,进行登录/注册这个用户
            R r = memberFeignService.oauth2Login(socialUser);
            if (r.getCode() == 0) {
                MemberResponseVo data = r.getData("data", new TypeReference<MemberResponseVo>() {});
                log.info("登录成功!!! --> " + data);
                //1、第一次使用session;命令浏览器保存卡号。JSESSIONID这个cookie;
                // 以后浏览器访问哪个网站就会带上这个网站的cookie;
                //子域之间,例如gulimalL.com auth.gulimall.com order.gulimall.com之间发卡的时候(指定域名为父域名)，
                // 这样做之后,即使是子域系统发的卡,也能让父域直接使用。
                // 1、默认发的令牌。session="....abcd..."。作用域。当前域; (解决子域session共享问题)
                // 2.使用JSON的序列化方式来序列化对象数据到redis中
                session.setAttribute(AuthServerConstant.LOGIN_USER, data);

                //2、登录成功跳回首页
                return "redirect:http://gulimall.com";
            } else {
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }else {
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }

}
